package wzh.crm.ui.product;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import wzh.crm.R;
import wzh.crm.bl.BL;
import wzh.crm.vo.ProductVo;

public class ProductDetailsActivity extends AppCompatActivity {
    ProductVo model;
    boolean editable;

    EditText 产品名称;
    EditText 产品编号;
    EditText 标准单价;
    EditText 销售单位;
    Spinner 分类;
    EditText 产品介绍;
    EditText 备注;
    Button 保存;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_details);

        loadViews();
        readIntent();
        displayModel();

        if(!editable){
            disableEdit();
        }
    }

    void loadViews(){
        产品名称= (EditText) findViewById(R.id.产品名称);
        产品编号= (EditText) findViewById(R.id.产品编号);
        标准单价= (EditText) findViewById(R.id.标准单价);
        销售单位= (EditText) findViewById(R.id.销售单位);
        分类= (Spinner) findViewById(R.id.分类);
        产品介绍= (EditText) findViewById(R.id.产品介绍);
        备注= (EditText) findViewById(R.id.备注);
        保存= (Button) findViewById(R.id.保存);
    }

    void disableEdit(){
        产品名称.setFocusable(false);
        产品编号.setFocusable(false);
        标准单价.setFocusable(false);
        销售单位.setFocusable(false);
        分类.setFocusable(false);
        产品介绍.setFocusable(false);
        备注.setFocusable(false);
        保存.setVisibility(View.INVISIBLE);
    }

    void readIntent() {
        editable = getIntent().getBooleanExtra("EDITABLE", true);
        model = (ProductVo) getIntent().getSerializableExtra("MODEL");
        if (model == null) {
            model = new ProductVo();
        }
    }

    void updateModel() {
        model.setProductname(产品名称.getText().toString());
        model.setProductsn(产品编号.getText().toString());
        model.setStandardprice(Double.parseDouble(标准单价.getText().toString()));
        model.setSalesunit(销售单位.getText().toString());
        model.setClassification(分类.getSelectedItem().toString());
        model.setIntroduction(产品介绍.getText().toString());
        model.setProductremarks(备注.getText().toString());
    }

    void displayModel() {
        this.setTitle(model.getProductname());
        产品名称.setText(model.getProductname());
        产品编号.setText(model.getProductsn());
        标准单价.setText(""+model.getStandardprice());
        销售单位.setText(model.getSalesunit());
        产品介绍.setText(model.getIntroduction());
        备注.setText(model.getProductremarks());
    }

    public void delete(View view){
        AsyncTask<Object,Object,Object> asyncTask= new AsyncTask<Object, Object, Object>() {
            @Override
            protected Object doInBackground(Object... params) {
                try {
                    BL.productBL().delete(model);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return null;
            }
        };
        asyncTask.execute(new Object());
        this.finish();
    }

    public void save(View view){
        updateModel();
        AsyncTask<Object,Object,Object> asyncTask= new AsyncTask<Object, Object, Object>() {
            @Override
            protected Object doInBackground(Object... params) {
                try {
                    BL.productBL().save(model);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return null;
            }
        };
        asyncTask.execute(new Object());
        this.finish();
    }

}
